<template>
  <div class="app-container" style="font-size: 14px;font-weight: 500">
    <div class="filter-container">
      <el-input v-model="listQuery.title" :placeholder="$t('number.title_placeholder')" style="width: 200px;"
                class="filter-item"/>
      <el-select ref="typeOption" v-model="listQuery.type" clearable filterable placeholder="请选择赛选类型"
                 style="width: 300px">
        <el-option
            style="width: 300px"
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-date-picker v-model:start-date="listQuery.start_create_at"
                      type="date"
                      format="yyyy-MM-dd"
                      :placeholder="$t('number.created_at_start_placeholder')"
                      style="width: 200px;margin-left: 20px"
      />
      至
      <el-date-picker v-model:start-date="listQuery.end_create_at"
                      type="date"
                      format="yyyy-MM-dd"
                      :placeholder="$t('number.created_at_end_placeholder')"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList()"
                 style="margin-left: 20px">
        {{ $t('table.search') }}
      </el-button>
      <el-button type="primary" @click="getList">
        <svg-icon icon-class="refresh" class-name="card-panel-icon icon-refresh"/>
      </el-button>
    </div>

    <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :element-loading-text="$t('table.load')"
        :data="list"
        border
        fit
        size="medium"
        highlight-current-row
        @selection-change="handleSelectionChange"
        style="margin-top: 20px"
    >

      <el-table-column type="selection" align="center"/>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.title')">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('user.account')">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.country')">
        <template slot-scope="scope">
          {{ scope.row.country_name }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="赛选类型" :formatter="typeFormat" width="150"/>
      <el-table-column label="赛号数量">
        <template slot-scope="scope">
          {{ scope.row.total_num }}
        </template>
      </el-table-column>
      <el-table-column label="有效数量">
        <template slot-scope="scope">
          {{ scope.row.success_count }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.total_price')">
        <template slot-scope="scope">
          {{ scope.row.total_price }}
        </template>
      </el-table-column>
      <el-table-column label="进度">
        <template slot-scope="scope">
          <el-progress :percentage="scope.row.progress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.finish_at')">
        <template slot-scope="scope">
          {{ scope.row.end_at }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.created_at')">
        <template slot-scope="scope">
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column prop="status" :label="$t('task.status')" :formatter="setStatusFormat" width="150"/>
      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button link class="link" v-if="row.down_url && row.down_url != '' && row.type==0 && row.ext == 'zip'"
                     size="small"
                     @click="downloadFile(row)">下载
          </el-button>
          <el-button link class="link" v-if="row.down_url && row.down_url != '' && row.type==0 && row.ext == 'txt'"
                     size="small"
                     @click="downloadFileTxt(row)">
            下载
          </el-button>
          <el-popover v-if="row.status == 2  && row.type==1 "
                      placement="top"
                      width="200"
                      trigger="click">
            <el-select ref="typeOptionAct"
                       v-model="form.actDay"
                       clearable
                       filterable
                       placeholder="请选择活跃天数"
                       style="width: 120px">
              <el-option
                  style="width: 120px"
                  v-for="item in form.typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
            <div style="text-align: right; margin: 10px">
              <el-button size="mini" type="text">取消</el-button>
              <el-button type="primary" size="mini" @click="downloadFileTxt(row,1)">确定
              </el-button>
            </div>
            <el-button slot="reference">下载</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
  </div>
</template>

<script>
import {taskFiltrateListV2 as taskFiltrateList, getTaskResult} from '@/api/whats'
import Pagination from '@/components/Pagination'
import {create} from "@/api/dnd";

export default {
  name: 'filtrateList',
  components: {Pagination},
  data() {
    return {
      typeOptions: [
        {
          value: 2,
          label: '开通'
        }, {
          value: 1,
          label: '活跃'
        }
      ],
      formLabelWidth: '120px',
      list: null,
      total: 0,
      visible: false,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: "",
        type: "",
        start_create_at: "",
        end_create_at: "",
        sort: '+id'
      },
      form: {
        typeOptions: [
          {
            value: 1,
            label: '1天内活跃'
          },
          {
            value: 3,
            label: '3天内活跃'
          }, {
            value: 7,
            label: '7天内活跃'
          }, {
            value: 30,
            label: '30天内活跃'
          }, {
            value: 100,
            label: '全部活跃'
          }
        ],
        taskId: 0,
        actDay: ""
      },
      listLoading: true,
      multipleSelection: [],
      downloadLoading: false,
      filename: '',

      logData: [],
      logTotal: 0,
      logListQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      logListLoading: false,
      dialogPvVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    typeFormat(row, column, cellValue, index) {
      return {0: '开通', 1: '活跃', 2: '性别年龄'}[cellValue]
    },
    handleUpdate(row) {
      this.dialogPvVisible = true
      this.logListLoading = true
      this.form.taskId = row.id
    },
    downloadFile(row) {
      const url = row.down_url; // 文件的 URL 地址
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', ''); // 设置文件属性为下载
      document.body.appendChild(link); // 将元素插入到 DOM 中
      link.click(); // 模拟点击下载链接进行下载
      document.body.removeChild(link); // 下载完成后移除元素
    },
    downloadFileTxt(row) {
      getTaskResult({
        taskId: row.id,
        act: this.form.actDay
      }).then(response => {
        // 创建一个新的blob URL
        let filename = response.headers['filename']
        const blobUrl = window.URL.createObjectURL(new Blob([response.data]));
        // let fileName=dis.split('attachment;filename=')[1]
        // 创建一个链接元素
        const link = document.createElement('a');
        link.href = blobUrl;
        link.setAttribute('download', filename); // 设置下载文件名
        document.body.appendChild(link);

        // 触发下载
        link.click();

        // 清理
        document.body.removeChild(link);
        window.URL.revokeObjectURL(blobUrl);
      })
          .catch(error => {
            console.error('Download error:', error);
          });
    },
    getList() {
      this.listLoading = true
      taskFiltrateList({
        page: this.listQuery.page,
        limit: this.listQuery.limit,
        type: this.listQuery.type,
        title: this.listQuery.title,
        start_create_at: this.listQuery.start_create_at,
        end_create_at: this.listQuery.end_create_at,
      }).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    setStatusFormat(row, column, cellValue, index) {
      return {0: '待执行', 1: '执行中', 2: '已完成', 3: '失败', 4: '入库中'}[cellValue]
    }
  }
}
</script>
